import logging
import os
import platform
import re
import subprocess
import time
import traceback

import prettytable
from airtest.core.api import connect_device

from .exception import LocalPackageNotFoundException
# from .uiautomator_helper import U2Helper  # no use here
from .utils import Utils

logger = logging.getLogger(__name__)


class AdbTool(object):
    def __init__(self, adb_ip, device_name):
        self.device_name = device_name
        self.adb_ip = adb_ip
        self.command_path = 'adb -H {}'.format(adb_ip)
        self.command_args = '-s {}'.format(device_name)
        # self.u2helper = U2Helper(self.device_name)
        self.airtest_devcie = None
        pass

    @property
    def system(self):
        return platform.system()

    def output(self, p):
        if p.stdout:
            return Utils.deal_with_python_version(p.stdout.readlines())
        else:
            return Utils.deal_with_python_version(p.stderr.readlines())

    @property
    def adb_command(self):
        return '{} {} '.format(self.command_path, self.command_args)

    def push_file(self, source, target):
        logger.info('({}) 将文件 {} 发送到 {}'.format(self.device_name, source, target))
        cmd = '{} push --sync {} {}'.format(self.adb_command, source, target)
        p = Utils.command_execute(cmd)
        return self.output(p)

    def remove_file(self, file_name):
        logger.info('({}) 开始删除文件 <{}>'.format(self.device_name, file_name))
        if file_name in ['/', '/sdcard'] or not file_name.startswith('/sdcard/'):
            logger.error('({}) 文件名类型不对，无法删除!'.format(self.device_name))
            return False
        cmd = '{} shell rm -rf {}'.format(self.adb_command, file_name)
        p = Utils.command_execute(cmd)
        r = self.output(p)
        if len(r) > 0:
            logger.info(r)
        return True

    def connect_remote_device(self, remote_device_id):
        cmd = '{} connect {}'.format(self.adb_command, remote_device_id)
        p = Utils.command_execute(cmd)
        return self.output(p)

    def pull_file(self, source, target):
        logger.info('({}) 将文件 {} 下载到 {}'.format(self.device_name, source, target))
        cmd = '{} pull {} {}'.format(self.adb_command, source, target)
        p = Utils.command_execute(cmd)
        return self.output(p)

    def get_android_version(self):
        logger.info("{} 获取系统版本".format(self.device_name))
        cmd = '{} shell getprop ro.build.version.release'.format(self.adb_command)

    @property
    def check_screen_locked(self):
        """
        linux : adb shell dumpsys window policy | grep mInputRestricted

        mInputRestricted=false  解锁   mInputRestricted=true 锁屏/黑屏
        """
        mInputRestricted = True
        mShowingLockscreen = True
        logger.info('{} 检测锁屏状态'.format(self.device_name))
        cmd = '{} shell dumpsys window policy | grep mInputRestricted'.format(self.adb_command)
        p = Utils.command_execute(cmd)
        stdout, stderr = p.communicate()
        logger.info('{} mInputRestricted 检测锁屏状态 结果： {}'.format(self.device_name, stdout))
        if b"false" in stdout:
            mInputRestricted = True
        else:
            mInputRestricted = False

        cmd = '{} shell dumpsys window policy | grep mShowingLockscreen'.format(self.adb_command)
        p = Utils.command_execute(cmd)
        stdout, stderr = p.communicate()
        logger.info('{} mShowingLockscreen 检测锁屏状态 结果： {}'.format(self.device_name, stdout))
        if b"false" in stdout:
            mShowingLockscreen = True
        else:
            mShowingLockscreen = False

        return mInputRestricted or mShowingLockscreen

    def start_activity(self, package_name, activity_name):
        try:
            activity_name = '{}/{}'.format(package_name, activity_name)
            logger.info('({}) 启动 activity : {}'.format(self.device_name, activity_name))
            cmd = '{} shell am start -W -n {}'.format(self.adb_command, activity_name)
            p = Utils.command_execute(cmd)
            result = self.output(p)
            logger.info(result)
            time.sleep(10)
            current_activity = self.get_current_activity()
            if current_activity == activity_name:
                logger.info('({}) activity 已经启动成功'.format(self.device_name))
                return True
            return result
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())

    def start_app(self, package_name, activity_name):
        try:
            logger.info('({}) 启动 APP : {}/{}'.format(self.device_name, package_name, activity_name))
            cmd = '{} shell am start -S -W {}/{}'.format(self.adb_command, package_name, activity_name)
            p = Utils.command_execute(cmd)
            stdout, stderr = p.communicate()
            logger.info(stdout)
            if p.returncode == 0:
                return True
            else:
                return False
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())

    def start_app_time(self, package_name, activity_name):
        try:
            logger.info('({}) 启动 APP : {}/{}'.format(self.device_name, package_name, activity_name))
            cmd = '{} shell am start -S -W {}/{}'.format(self.adb_command, package_name, activity_name)
            p = Utils.command_execute(cmd)
            stdout, stderr = p.communicate()
            logger.info(stdout)

            matcher = re.search(r"TotalTime:\s*(\d+)", stdout.decode('utf-8'))
            t = 0
            if matcher:
                t = int(matcher.group(1))
                t = t / 1000

            if p.returncode == 0:
                return True, t
            else:
                return False, t
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())
            return False, 0

    def close_app(self, package_name):
        try:
            logger.info('关闭APP：{}'.format(package_name))
            cmd = '{} shell am force-stop {}'.format(self.adb_command, package_name)
            p = Utils.command_execute(cmd)
            stdout, stderr = p.communicate()
            logger.info(stdout)
            if p.returncode == 0:
                return True
            else:
                return False
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())

    def clear_logcat(self):
        logger.info('({}) 清除 logcat log'.format(self.device_name))
        cmd = '{} logcat -c'.format(self.adb_command)
        p = Utils.command_execute(cmd)
        return p

    def get_cache_logcat(self, type, path):
        try:
            logger.info('({}) 获取缓存中的logcat 生成logcat.txt'.format(self.device_name))
            cmd = '{} logcat -b {} -d > {}'.format(self.adb_command, type, path)
            if(type == None):
                cmd = '{} logcat -d > {}'.format(self.adb_command,  path)
            p = Utils.command_execute(cmd)
            while p.poll() is None:
                time.sleep(1)
            return path
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())

    def start_logcat(self, target):
        logger.info('({}) 打开 logcat log'.format(self.device_name))
        # cmd = '{} logcat -v time > {}'.format(self.adb_command, target)
        # p = Utils.command_execute(cmd)
        return None

    def back_to_home(self):
        cmd = '{} shell input keyevent 3'.format(self.adb_command)
        p = Utils.command_execute(cmd)
        return self.output(p)

    def run_monkey(self, monkey_command):
        try:
            cmd = '{} {}'.format(self.adb_command, monkey_command)
            p = Utils.command_execute(cmd, 20)
            return p
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())

    def run_performance(self, performance_command):
        try:
            cmd = '{}'.format(performance_command)
            p = Utils.command_execute(cmd)
            return p
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())

    def install_package(self, local_package_path, package_name, force_install=True, pipe_file=None):
        logger.info('({}) 开始安装包 ： {}'.format(self.device_name, local_package_path))
        try:
            if not os.path.exists(local_package_path):
                raise LocalPackageNotFoundException
            if force_install:
                cmd = '{} install -r -g {}'.format(self.adb_command, local_package_path)
            else:
                cmd = '{} install -g {}'.format(self.adb_command, local_package_path)

            start_time = time.time()
            time_out = 8 * 60
            if pipe_file is None:
                p = Utils.command_execute(cmd)
            else:
                p = subprocess.Popen(cmd, shell=True, stdout=open(pipe_file, "w+"), stderr=subprocess.STDOUT,
                                     executable="/bin/bash")
            while p.poll() is None:
                if (time.time() - start_time) > time_out:
                    logger.info('({}) "安装APK超时"'.format(self.device_name))
                    p.kill()
                time.sleep(5)
            if pipe_file is None:
                stdout = self.output(p)
            else:
                with open(pipe_file, "r") as f:
                    stdout = f.read()
                    f.close()
            logger.info('({}) {}'.format(self.device_name, stdout))

            if self.check_package_installed(package_name):
                # for r in result:
                #     if 'Success' in r:
                #         logger.info('({}) 安装 {} 成功'.format(self.device_name, local_package_path))
                #         return True
                return True
            logger.error('({}) 安装 {} 失败'.format(self.device_name, local_package_path))
            return False
        except Exception as e:
            logger.error('({}) 安装 {} 失败'.format(self.device_name, local_package_path))
            logger.error(e)
            return False

    def install_apk_with_pm(self, local_package_path, package_name, network_path = None,force_install=True, pipe_file=None):
        try:
            target = "/data/local/tmp"
            # 1.解析url，得到apkname
            if(network_path and "192.168" in self.device_name ):
                logger.info('apk 链接：{}'.format(network_path))
                apk_name  = os.path.basename(network_path)
                    # 2.构建启动下载命令，下载apk
                # adb shell am start -n com.steinwurf.adbjoinwifi/.MainActivity3 -e url  http://192.168.6.241:8069/monkey_package/3584cb3ae1bc1cb486fbb133a5c51a01.apk
                download_cmd = "{} shell am start -S -n com.steinwurf.adbjoinwifi/.MainActivity3 -e url {}".format(self.adb_command,network_path)
                Utils.command_execute(download_cmd)
                # 暂停5秒等待下载器准备
                time.sleep(5)
                download_state = "";

                start_time = time.time()
                time_out = 25 * 60
                count = 0
                while count<5:
                    try:
                        if (time.time() - start_time) > time_out:
                            logger.info('({}) "下载APK超时"'.format(self.device_name))
                            return False
                        current_activity = self.get_current_activity()
                        # logger.info('({}) apk 当前界面：{}'.format(self.device_name,current_activity))
                        # if((current_activity != None) and ("com.steinwurf.adbjoinwifi.MainActivity3" not in current_activity)):
                        #     Utils.command_execute(download_cmd)
                        status_cmd = "{} shell cat /sdcard/com.steinwurf.adbjoinwifi/tcloud.txt".format(self.adb_command)
                        p = Utils.command_execute(status_cmd)
                        if(p !=None):
                            line= str(p.stdout.readline())
                            if("success" in line or "fail" in line):
                                download_state = line
                                break
                        time.sleep(10)
                    except Exception as ex:
                        logger.error(ex)
                        count = count+1

                # 3.用shell把下载的apk从sdcard 复制到/data/local/tmp/
                #  adb shell cp -r /sdcard/com.steinwurf.adbjoinwifi/3584cb3ae1bc1cb486fbb133a5c51a01.apk /data/local/tmp/
                if("success" in download_state):
                    copy_cmd = "{} shell cp -r /sdcard/com.steinwurf.adbjoinwifi/{} {}".format(self.adb_command,apk_name,target)
                    p =Utils.command_execute(copy_cmd,60)
                    while p.poll() is None:
                        continue
                elif("fail" in download_state):
                    logger.error('({}) apk 下载失败：{}'.format(self.device_name,network_path))
                    return False
                else:
                    logger.error('({}) 奇怪的下载状态：{}'.format(self.device_name,download_state))
                    return False

            else:
                apk_name  = os.path.basename(local_package_path)
                if not os.path.exists(local_package_path):
                    raise LocalPackageNotFoundException
                stdout = self.push_file(source=local_package_path,target=target)
                logger.info('({}) {}'.format(self.device_name, stdout[- 1]))
                
            device_apk_path = "{}/{}".format(target,apk_name)

            if force_install:
                cmd = '{} shell pm install -r -g {}'.format(self.adb_command, device_apk_path)
            else:
                cmd = '{} shell pm install -g {}'.format(self.adb_command, device_apk_path)
            
            start_time = time.time()
            time_out = 8 * 60
            if pipe_file is None:
                p = Utils.command_execute(cmd)
            else:
                p = subprocess.Popen(cmd, shell=True, stdout=open(pipe_file, "w+"), stderr=subprocess.STDOUT,
                                    executable="/bin/bash")
            while p.poll() is None:
                if (time.time() - start_time) > time_out:
                    logger.info('({}) "安装APK超时"'.format(self.device_name))
                    p.kill()
                time.sleep(5)
            if pipe_file is None:
                stdout = self.output(p)
            else:
                with open(pipe_file, "r") as f:
                    stdout = f.read()
                    f.close()
            logger.info('({}) install ret: {}'.format(self.device_name, stdout))

            if self.check_package_installed(package_name) or "Success" in str(stdout): 
                # for r in result:
                #     if 'Success' in r:
                #         logger.info('({}) 安装 {} 成功'.format(self.device_name, local_package_path))
                #         return True
                return True
            logger.error('({}) 安装 {} 失败'.format(self.device_name, device_apk_path))
            return False
        except Exception as e:
            logger.error('({}) 安装 {} 失败'.format(self.device_name, device_apk_path))
            logger.error(e)
            return False


    def install_app_airtest(self, local_package_path, package_name, force_install=True):
        try:
            if self.airtest_devcie is None:
                device_uri = "android://%s:%s/%s" % (self.adb_ip, "5037", self.device_name)
                logger.error('{} airtest 安装,连接设备 {}'.format(self.device_name, device_uri))
                self.airtest_devcie = connect_device(device_uri)
            if self.airtest_devcie is None:
                logger.error('{} airtest 安装,连接设备失败'.format(self.device_name))
                return False
            logger.info('({})airtest 开始安装包 ： {}'.format(self.device_name, local_package_path))
            stdout = self.airtest_devcie.install_app(local_package_path, force_install, install_options=['-g'])
            logger.info('({}) {}'.format(self.device_name, stdout))
            if self.check_package_installed(package_name):
                return True
            return False
        except Exception as e:
            logger.error('({}) 安装 {} 失败'.format(self.device_name, local_package_path))
            logger.error(e)
            return False

    def uninstall_package(self, package_name):
        logger.info('({}) 开始卸载 ：{}'.format(self.device_name, package_name))
        try:
            if self.check_package_installed(package_name):
                cmd = '{} uninstall {}'.format(self.adb_command, package_name)
                p = Utils.command_execute(cmd)
                result = self.output(p)
                logger.info('({}) {}'.format(self.device_name, result))
                for r in result:
                    if 'Success' in r:
                        logger.info('({}) 卸载 {} 成功'.format(self.device_name, package_name))
                        return True
                logger.error('({}) 卸载 {} 失败 : '.format(self.device_name, package_name))
                return False
            else:
                logger.info('({}) 设备没有安装 {}, 不需要卸载'.format(self.device_name, package_name))
                return True
        except Exception as e:
            logger.error('({}) 卸载 {} 失败 : '.format(self.device_name, package_name))
            return e

    def get_installed_packages(self, show_table=False):
        try:
            cmd = '{} shell pm list packages'.format(self.adb_command)
            p = Utils.command_execute(cmd)
            package_list = self.output(p)
            if show_table:
                logger.info('({}) 获取所有的已安装的包'.format(self.device_name))
                table_packages = prettytable.PrettyTable()
                table_packages.field_names = ["id", "package name"]
                for i, package in enumerate(package_list):
                    row = [i, package]
                    table_packages.add_row(row)
                logger.info('({}) \n {}'.format(self.device_name, table_packages))
            return package_list
        except Exception as e:
            logger.error(e)
            return e

    def check_package_installed(self, package_name):
        for package in self.get_installed_packages():
            if package_name in package:
                return True

        return False

    def check_adb(self):
        if "ANDROID_HOME" in os.environ:
            if self.system == "Windows":
                path = os.path.join(os.environ["ANDROID_HOME"], "platform-tools", "adb.exe")
                if os.path.exists(path):
                    self.command_path = path
                else:
                    raise EnvironmentError("Adb not found in $ANDROID_HOME path: {}".format(os.environ["ANDROID_HOME"]))
            else:
                path = os.path.join(os.environ["ANDROID_HOME"], "platform-tools", "adb")
                if os.path.exists(path):
                    self.command_path = path
                else:
                    raise EnvironmentError(
                        "Adb not found in $ANDROID_HOME path: {}.".format(os.environ["ANDROID_HOME"]))
        else:
            raise EnvironmentError("Adb not found in $ANDROID_HOME path: {}".format(os.environ["ANDROID_HOME"]))

    def get_current_application(self):
        return Utils.command_execute(
            '{} shell dumpsys window w | grep mCurrentFocus'.format(self.adb_command))

    def get_current_package(self):
        p = self.get_current_application()
        result = self.output(p)
        if len(result) > 0:
            return result[0].split(' ')[-1].split('/')[0]
        return None

    def get_package_version(self, package_name):
        logger.info('({}) 获取 安装包 版本信息'.format(self.device_name))
        if self.check_package_installed(package_name):
            cmd = '{} shell dumpsys package {} | grep versionName'.format(self.adb_command, package_name)
            p = Utils.command_execute(cmd)
            r = self.output(p)
            if len(r) > 0:
                temp = r[0].split('=')
                if len(temp) > 0:
                    version = temp[1].strip()
                    logger.info('({}) 版本是 [{}] '.format(self.device_name, version))
                    return version
            return ''
        else:
            logger.info('({}) {} 没有安装!'.format(self.device_name, package_name))
            return None

    def get_current_activity(self):
        p = self.get_current_application()
        result = self.output(p)
        if len(result) > 0:
            names = result[0].split(' ')
            if len(names) > 1:
                activity_name = names[-1]
                if '/' in activity_name:
                    activity = activity_name.split('/')
                    return activity[1].strip() if len(activity) > 1 else activity
        return None

    def get_process(self, package_name):
        if self.system is "Windows":
            pid_command = Utils.command_execute(
                "{} shell ps | grep {}$".format(self.adb_command, package_name)).stdout.readlines()
        else:
            pid_command = Utils.command_execute(
                "{} shell ps | grep -w {}".format(self.adb_command, package_name)).stdout.readlines()

        return Utils.deal_with_python_version(pid_command)

    def process_exists(self, package_name):
        process = self.get_process(package_name)
        return package_name in process

    def get_pid(self, package_name):
        pid_command = self.get_process(package_name)
        if pid_command == '':
            logger.info("The process doesn't exist.")
            return pid_command
        req = re.compile(r"\d+")
        result = str(pid_command).split()
        result.remove(result[0])
        return req.findall(" ".join(result))[0]

    def get_uid(self, pid):
        result = Utils.command_execute("{} shell cat /proc/{}/status".format(self.adb_command, pid)).stdout.readlines()
        result = Utils.deal_with_python_version(result)
        for i in result:
            if 'uid' in i.lower():
                return i.split()[1]

    def get_battery_level(self):
        result = Utils.command_execute('{} shell dumpsys battery'.format(self.adb_command)).stdout.readlines()
        result = Utils.deal_with_python_version(result)
        for r in result:
            if 'level' in r:
                return int(r.split(':')[1])
        return 0

    def get_flow_data_tcp(self, uid):
        tcp_rcv = \
            Utils.command_execute("{} shell cat proc/uid_stat/{}/tcp_rcv".format(self.adb_command, uid)).read().split()[
                0]
        tcp_snd = \
            Utils.command_execute("{} shell cat proc/uid_stat/{}/tcp_snd".format(self.adb_command, uid)).read().split()[
                0]
        return tcp_rcv, tcp_snd

    def get_adb_version(self):
        cmd = '{} version'.format(self.adb_command)
        p = Utils.command_execute(cmd)
        return self.output(p)

    def get_device_list(self):
        try:
            cmd = '{} devices'.format(self.command_path)
            p = Utils.command_execute(cmd)
            result = self.output(p)
            devices = []
            if len(result) > 0 and result[0].startswith('List'):
                for line in result[1:]:
                    if line in ['\n'] or 'un' in line:
                        continue
                    try:
                        device = line.strip().replace('\t', '').split('device')[0]
                    except Exception as e:
                        device = None
                        logger.error(e)
                    if device not in [None, '\n', '\t']:
                        devices.append(device)
            return devices
        except Exception as e:
            logger.info(e)
            logger.info(traceback.format_exc())
            return []

    def check_device_connected(self, device):
        return device in self.get_device_list()

    def get_crash_dump_log(self):
        try:
            cmd = '{} shell cat /sdcard/MonkeyLog/crash-dump.log'.format(self.adb_command)
            p = Utils.command_execute(cmd)
            return self.output(p)
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())

    def unlock_screen(self):
        try:
            cmd = '{} shell input swipe 300 1600 300 20'.format(self.adb_command)
            p = Utils.command_execute(cmd)
            p.communicate()
            return True
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())

    def wakeup_screen(self):
        """
        唤醒屏幕,判断： screenState=SCREEN_STATE_OFF  息屏
                        screenState=SCREEN_STATE_ON  亮屏
        """
        try:
            cmd_screen_state = '{} shell dumpsys window policy | grep screenState'.format(self.adb_command)
            p = Utils.command_execute(cmd_screen_state)
            stdout, stderr = p.communicate()
            if b"SCREEN_STATE_OFF" in stdout:
                cmd = '{} shell input keyevent 26'.format(self.adb_command)
                p = Utils.command_execute(cmd)
                p.communicate()
            return True
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())

    def get_bug_report_log(self, log_path):
        try:
            cmd = '{} shell bugreport > {}'.format(self.adb_command, log_path)
            p = Utils.command_execute(cmd,timeout=100)
            while (p!=None and p.poll()) is None:
                time.sleep(1)

            for file_name in os.listdir("./"):
                if file_name.startswith('bugreport-') and file_name.endswith('.zip'):
                    return file_name
            return log_path
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())

    def reset_bug_report_log(self):
        try:
            logger.info('reset battery stats log now...')
            cmd = '{} shell dumpsys batterystats --reset'.format(self.adb_command)
            p = Utils.command_execute(cmd)
            return self.output(p)
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())

    def set_system_default_input(self, key):
        try:
            if key:
                logger.info('try to set system default input to qjp')
                key_of_qjp = key
                cmd = '{} shell ime enable {}'.format(self.adb_command, key_of_qjp)
                p = Utils.command_execute(cmd)
                logger.info(self.output(p))
                time.sleep(5)
                cmd = '{} shell ime set {}'.format(self.adb_command, key_of_qjp)
                p = Utils.command_execute(cmd)
                logger.info(self.output(p))
                time.sleep(5)
                return True
        except Exception as e:
            logger.error(e)

    def get_memory_info(self, package_name):
        try:
            logger.info('try to get memory information')
            cmd = '{} shell dumpsys meminfo {}'.format(self.adb_command, package_name)
            p = Utils.command_execute(cmd)
            lines = self.output(p)
            # lines = Utils.deal_with_python_version(lines)
            heap_size, heap_alloc = 0, 0
            for line in lines:
                if 'Native Heap' in line:
                    temp = line.split()
                    if len(temp) == 9:
                        heap_size = temp[-3]
                        heap_alloc = temp[-2]
                    break
            heap_size = int(heap_size) if heap_size != '' else 0
            heap_alloc = int(heap_alloc) if heap_alloc != '' else 0
            return heap_size, heap_alloc
        except Exception as e:
            # logger.error(e)
            # return 0, 0
            raise e

    def get_cpu(self, package_name_old):
        try:
            logger.info('try to get cpu information')
            if len(package_name_old) > 15:
                package_name = "{}+".format(package_name_old[:15])
            else:
                package_name = package_name_old

            cmd = '{} shell "top -n 1 | grep {}" '.format(self.adb_command, package_name)

            p = Utils.command_execute(cmd)
            lines = self.output(p)
            # lines = Utils.deal_with_python_version(lines)
            cpu = rss = 0
            for line in lines:
                logger.info(line)
                if package_name in line and f'{package_name}:' not in line:
                    # temp = re.findall(r'.* (\w+)% .* (\w+)K (\w+)K .* {}'.format(package_name), line)
                    temp = re.findall(r'.* .* .* .* .* (\w+)M (\w+)M .* (\w+) .* .* {}'.format(package_name), line)
                    cpu = temp[0][2]
                    rss = temp[0][0]
                    break
            cpu = int(cpu) if cpu != '' else 0
            rss = int(rss) if rss != '' else 0
            return cpu, rss * 1024
        except Exception as e:
            # logger.error(e)
            # return 0, 0
            raise e

    def clear_package_cache_data(self, package_name):
        try:
            logger.info('try to clear cache data information {}'.format(package_name))
            cmd = '{} shell pm clear {}'.format(self.adb_command, package_name)
            lines = Utils.command_execute(cmd)
            return lines
        except Exception as e:
            logger.error(e)
            

    def shell(self, command):
        try:
            cmd = '{} shell {}'.format(self.adb_command, command)
            lines = Utils.command_execute(cmd)
            return lines
        except Exception as e:
            logger.error(e)
